function Bleeding takes nothing returns nothing
	local timer bleedingTimer = GetExpiredTimer()
	local integer timerId = GetHandleId(bleedingTimer)
	local unit bleedingUnit = LoadUnitHandle(udg_HT , timerId , 3)
	local real hp = GetUnitState(bleedingUnit , UNIT_STATE_LIFE)
	//--
	if UnitHasBuffBJ(bleedingUnit , 'B002') and hp > 0.0 then
		call SetUnitState(bleedingUnit , UNIT_STATE_LIFE , hp - 2.0)
		call DestroyEffect(AddSpecialEffectTarget("Objects\\Spawnmodels\\Other\\HumanBloodCinematicEffect\\HumanBloodCinematicEffect.mdl" , bleedingUnit , "chest"))
	else
		call PauseTimer(bleedingTimer)
		call FlushChildHashtable(udg_HT , timerId)
		call DestroyTimer(bleedingTimer)
	endif
	//--
	set bleedingTimer = null
	set bleedingUnit = null
endfunction

function ModifyDamageSetHp takes nothing returns nothing
	local timer setHpTimer = GetExpiredTimer()
	local integer timerId = GetHandleId(setHpTimer)
	local unit unitToSetHp = LoadUnitHandle(udg_HT , timerId , 3)
	//--
	call UnitRemoveAbility(unitToSetHp , 'A00F')
	call SetUnitState(unitToSetHp , UNIT_STATE_LIFE , LoadReal(udg_HT , timerId , 4))
	call PauseTimer(setHpTimer)
	call FlushChildHashtable(udg_HT , timerId)
	call DestroyTimer(setHpTimer)
	//--
	set setHpTimer = null
	set unitToSetHp = null
endfunction

function DamageModify takes nothing returns real
	local unit unitUnderDamage = GetTriggerUnit()
	local integer unitId = GetHandleId(unitUnderDamage)
	local timer setHpTimer = LoadTimerHandle(udg_UnitDataHashTable , unitId , StringHash("DAMAGE_MODIFY_SET_HP_TIMER"))
	local integer timerId = 0
	local real hpNow = GetUnitState(unitUnderDamage , UNIT_STATE_LIFE)
	local real hpMax = GetUnitState(unitUnderDamage , UNIT_STATE_MAX_LIFE)
	local real damage = GetEventDamage()
	local real defendPercent = 0.0
	local real blockProbability = 0.0
	local real newDamage = 0.0
	local unit attacker = GetEventDamageSource()
	local real attackerAdditionalDamagePercent = LoadReal(udg_UnitDataHashTable , GetHandleId(attacker) , StringHash("ADDITIONAL_DAMAGE_PERCENT"))
	//--
	if setHpTimer != null then
		call ModifyDamageSetHp()
	endif
	
	set defendPercent = LoadReal(udg_UnitDataHashTable , GetHandleId(unitUnderDamage) , StringHash("UNIT_DEFEND_PERCENT"))
	if defendPercent < 0.0 then
		set defendPercent = 0.0
	elseif defendPercent > 100.0 then
		set defendPercent = 100.0
	endif
	
	set blockProbability = LoadReal(udg_UnitDataHashTable , GetHandleId(unitUnderDamage) , StringHash("BLOCK_DAMAGE_PROBABILITY"))
	if blockProbability < 0.0 then
		set blockProbability = 0.0
	elseif blockProbability > 100.0 then
		set blockProbability = 100.0
	endif
	
	if blockProbability > 0.0 and GetRandomReal(0.0 , 100.0) <= blockProbability then
		set newDamage = 0.0
		call CreateChineseTextTagOnUnit("格擋" , 0.02 , unitUnderDamage , 50.0 , 255 , 255 , 255 , 0.0 , 0.03 , false , 1.0 , 0.5)
	else
		set newDamage = damage * (1.0 - 0.01 * defendPercent) * (1.0 + 0.01 * attackerAdditionalDamagePercent)
		if newDamage > 0.0 then
			call CreateTextTagOnUnit(I2S(R2I(newDamage)) , 0.02 , unitUnderDamage , 50.0 , 255 , 255 , 255 , 0.0 , 0.03 , false , 1.0 , 0.5)
		endif
	endif
	
	if (hpNow + damage - newDamage <= hpMax) then
		call SetUnitState(unitUnderDamage , UNIT_STATE_LIFE , hpNow + damage - newDamage)
	else
		call UnitAddAbility(unitUnderDamage , 'A00F')
		set setHpTimer = CreateTimer()
		call SaveTimerHandle(udg_UnitDataHashTable , unitId , StringHash("DAMAGE_MODIFY_SET_HP_TIMER") , setHpTimer)
		set timerId = GetHandleId(setHpTimer)
		call SaveUnitHandle(udg_HT , timerId , 3 , unitUnderDamage)
		call SaveReal(udg_HT , timerId , 4 , hpNow - newDamage)
		call TimerStart(setHpTimer , 0.0 , false , function ModifyDamageSetHp)
	endif
	set unitUnderDamage = null
	set setHpTimer = null
	return newDamage
endfunction

function DamageEvent takes nothing returns nothing
	local unit damagedUnit = GetTriggerUnit()
	local real damage = 0.0
	local unit attacker = GetEventDamageSource()
	local player attackerPlayer = GetOwningPlayer(attacker)
	local integer level = 0
	local real successProbability = 0.0
	local timer bleedingTimer = null
	local integer timerId = 0
	local unit effectUnit = null
	//--
	set damage = DamageModify()
	
	if damage > 0.0 and IsUnitEnemy(damagedUnit , attackerPlayer) and not udg_IsAbilityDamage and not IsUnitType(damagedUnit , UNIT_TYPE_STRUCTURE) then	
		//恐龍造成出血
		set level = GetUnitAbilityLevel(attacker , 'A00G')
		if level > 0 and not UnitHasBuffBJ(damagedUnit , 'B002') then
			set successProbability = 0.05 * I2R(level)
			if GetRandomReal(0.0 , 1.0) < successProbability then
				call UnitAddAbility(damagedUnit , 'A00H')
				set bleedingTimer = CreateTimer()
				set timerId = GetHandleId(bleedingTimer)
				call SaveUnitHandle(udg_HT , timerId , 3 , damagedUnit)
				call TimerStart(bleedingTimer , 4.0 , true , function Bleeding)
			endif
		endif	
	endif
	//--
	set damagedUnit = null
	set attacker = null
	set attackerPlayer = null
	set bleedingTimer = null
	set effectUnit = null
endfunction

function GlobalUnitDead takes nothing returns nothing
	local trigger deadEventTrigger = GetTriggeringTrigger()
	local integer deadEventTriggerID = GetHandleId(deadEventTrigger)
	local triggeraction deadEventTriggerAction = LoadTriggerActionHandle(udg_TriggerDataHashTable , deadEventTriggerID , StringHash("ACTION"))
	local trigger damageEventTrigger = LoadTriggerHandle(udg_TriggerDataHashTable, deadEventTriggerID , StringHash("DAMAGE_EVENT_TRIGGER"))
	local triggeraction damageEventTriggerAction = LoadTriggerActionHandle(udg_TriggerDataHashTable , deadEventTriggerID , StringHash("DAMAGE_EVENT_TRIGGER_ACTION"))
	//--
	call FlushChildHashtable(udg_TriggerDataHashTable , deadEventTriggerID)
	call TriggerRemoveAction(damageEventTrigger , damageEventTriggerAction)
	call DestroyTrigger(damageEventTrigger)
	call TriggerRemoveAction(deadEventTrigger , deadEventTriggerAction)
	call DestroyTrigger(deadEventTrigger)
	//--
	set deadEventTrigger = null
	set deadEventTriggerAction = null
	set damageEventTrigger = null
	set damageEventTriggerAction = null
endfunction

function XplayerUnitDamagedCreateTrigger takes nothing returns nothing
	local trigger damageEventTrigger = null
	local unit newUnit = GetTriggerUnit()
	local trigger deadEventTrigger = null
	local integer deadEventTriggerID = 0
	//--
	if (IsUnitInForce(newUnit , udg_PlayerForce) or GetOwningPlayer(newUnit) == Player(11)) and GetUnitAbilityLevel(newUnit , 'Aloc') == 0 then
		set deadEventTrigger = CreateTrigger()
		set deadEventTriggerID = GetHandleId(deadEventTrigger)
		call TriggerRegisterUnitEvent(deadEventTrigger , newUnit , EVENT_UNIT_DEATH)
		call SaveTriggerActionHandle(udg_TriggerDataHashTable , deadEventTriggerID , StringHash("ACTION") , TriggerAddAction(deadEventTrigger , function GlobalUnitDead))
		
		set damageEventTrigger = CreateTrigger()
		call TriggerRegisterUnitEvent(damageEventTrigger , newUnit , EVENT_UNIT_DAMAGED)
		call SaveTriggerHandle(udg_TriggerDataHashTable, deadEventTriggerID , StringHash("DAMAGE_EVENT_TRIGGER") , damageEventTrigger)
		call SaveTriggerActionHandle(udg_TriggerDataHashTable , deadEventTriggerID , StringHash("DAMAGE_EVENT_TRIGGER_ACTION") , TriggerAddAction(damageEventTrigger , function DamageEvent))
	endif
	//--
	set damageEventTrigger = null
	set newUnit = null
endfunction

function InitTrig_XplayerUnitDamaged takes nothing returns nothing
	local region worldRegion = CreateRegion()
	//--
	call RegionAddRect(worldRegion , GetWorldBounds())
	set gg_trg_XplayerUnitDamaged = CreateTrigger()
	call TriggerRegisterEnterRegion(gg_trg_XplayerUnitDamaged , worldRegion , null)
	call TriggerAddAction(gg_trg_XplayerUnitDamaged , function XplayerUnitDamagedCreateTrigger)
endfunction